package org.spring.springboot.tools;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;

public class serializeJson {
	
	
	public static Object getObjFromJsonArrStr(String source,Class beanClass)
    {
        JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(source);
        return  JSONObject.toBean(jsonObject,beanClass);        
    }
    
    public static Object getObjFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) 
    {  
        JSONObject jsonObj = JSONObject.fromObject(jsonArrStr);  
                return JSONObject.toBean(jsonObj, clazz, classMap);            
    }
  
     
    @SuppressWarnings("unchecked")
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz) {  
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);  
        List list = new ArrayList();  
        for (int i = 0; i < jsonArr.size(); i++) 
        {  
            list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz));  
        }  
        return list;  
    } 
  
     
    @SuppressWarnings("unchecked")
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) 
    {  
       JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);  
       List list = new ArrayList();  
       for (int i = 0; i < jsonArr.size(); i++) 
       {           
           list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz, classMap));  
       }  
       return list;  
    }
          
     
    @SuppressWarnings("unchecked")
    public static String getJsonArrStrFromList(Object obj) 
    { 
        //���ؽY��
        String jsonStr = null;  
      
        //�п�
        if (obj == null) {  
            return "{}";  
        }  
        //Json����      
       JsonConfig jsonCfg = new JsonConfig();  
       JsonValueProcessor jsonProcessor = new DateJsonValueProcessor();
        //ע�����ڴ���??  
       jsonCfg.registerJsonValueProcessor(Date.class, jsonProcessor);
         //jsonCfg ����
    
       jsonCfg.setIgnoreDefaultExcludes(false);       
       jsonCfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);     
       jsonCfg.setExcludes(new String[]{//ֻҪ����������飬ָ��������Щ�ֶ�???    
//       "l2Interfaces",    
//       "OSInstalled",    
         "message",    
       });    
        //�ж��Ƿ���list
        if (obj instanceof Collection || obj instanceof Object[]) {  
            //jsonStr = JSONArray.fromObject(obj).toString();  
            jsonStr = JSONSerializer.toJSON(obj,jsonCfg).toString();
        } else {  
        	 jsonStr = JSONSerializer.toJSON(obj,jsonCfg).toString();
            //jsonStr = JSONObject.fromObject(obj).toString();  
        }     
        return jsonStr; 
    }  
  //Date�������͵�Jsonֵ������,���Զ���??Щ��Ĵ���??,��json�ṩ��һ����չ��??
    private static class DateJsonValueProcessor implements JsonValueProcessor {
     
        private String format = "yyyy-MM-dd";   
        private SimpleDateFormat sdf = new SimpleDateFormat(format);   
     
        //���������???
        @Override
        public Object processArrayValue(Object value, JsonConfig jsonConfig) {
            return this.process(value);
        }
     
        //��������???
        @Override
        public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
            return this.process(value);
        }
         
        //����Date���ͷ��ص�Json��???
        private Object process(Object value) {
            if (value == null) {
                return "";
            } else if (value instanceof Date)
                return sdf.format((Date) value);
            else {
                return value.toString();
            }
        }
    }
}